package com.xjrsoft.common.runner;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.xjrsoft.common.enums.SystemConfigEnum;
import com.xjrsoft.common.utils.HttpContextUtils;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.common.utils.SystemConfigUtil;
import com.xjrsoft.config.*;
import com.xjrsoft.module.system.entity.SystemConfig;
import com.xjrsoft.module.system.model.DingtalkConfigModel;
import com.xjrsoft.module.system.model.EmailConfigModel;
import com.xjrsoft.module.system.model.MessageConfigModel;
import com.xjrsoft.module.system.model.WechatEnterpriseConfigModel;
import com.xjrsoft.module.system.service.ISystemConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @Author: tzx
 * @Date: 2024/1/17 9:55
 */
@Component
@AllArgsConstructor
@Slf4j
public class SystemConfigRunner implements CommandLineRunner {

    private final ISystemConfigService systemConfigService;

    @Override
    public void run(String... args) throws Exception {
        List<SystemConfig> list = systemConfigService.list();


        for (SystemConfig systemConfig : list) {
            //如果是邮件配置
            if (systemConfig.getConfigType() == SystemConfigEnum.EMAIL.getCode()) {
                EmailConfigModel emailConfigModel = JSONUtil.toBean(systemConfig.getConfigJson(), EmailConfigModel.class);
                if (emailConfigModel != null) {
                    SpringUtil.registerBean(MailAccount.class.getName(), SystemConfigUtil.createMailAccount(emailConfigModel));
                }
                continue;
            }
            //如果是短信配置
            if (systemConfig.getConfigType() == SystemConfigEnum.MESSAGE.getCode()) {
                MessageConfigModel messageConfigModel = JSONUtil.toBean(systemConfig.getConfigJson(), MessageConfigModel.class);
                if (messageConfigModel != null) {
                    SpringUtil.registerBean(XjrSmsConfig.class.getName(), SystemConfigUtil.createSmsConfig(messageConfigModel));
                }
                continue;
            }

            if (systemConfig.getConfigType() == SystemConfigEnum.WECHAT.getCode()) {
                WechatEnterpriseConfigModel wechatEnterpriseConfigModel = JSONUtil.toBean(systemConfig.getConfigJson(), WechatEnterpriseConfigModel.class);
                if (wechatEnterpriseConfigModel != null) {
                    SpringUtil.registerBean(WechatEnterpriseConfig.class.getName(), SystemConfigUtil.createWechatEnterpriseConfig(wechatEnterpriseConfigModel));
                }
                continue;
            }

            if (systemConfig.getConfigType() == SystemConfigEnum.DINGTALK.getCode()) {
                DingtalkConfigModel dingtalkConfigModel = JSONUtil.toBean(systemConfig.getConfigJson(), DingtalkConfigModel.class);
                if (dingtalkConfigModel != null) {
                    SpringUtil.registerBean(DingtalkConfig.class.getName(), SystemConfigUtil.createDingtalkConfig(dingtalkConfigModel));
                }
                continue;
            }

            if (systemConfig.getConfigType() == SystemConfigEnum.WEBHOOK.getCode()) {
                WebhookConfig webhookConfig = JSONUtil.toBean(systemConfig.getConfigJson(), WebhookConfig.class);
                if (webhookConfig != null) {
                    SpringUtil.registerBean(WebhookConfig.class.getName(), webhookConfig);
                }
                continue;
            }

            if (systemConfig.getConfigType() == SystemConfigEnum.WECHAT_OFFICIAL.getCode()) {
                WechatOfficalConfig wechatOfficalConfig = JSONUtil.toBean(systemConfig.getConfigJson(), WechatOfficalConfig.class);
                if (wechatOfficalConfig != null) {
                    SpringUtil.registerBean(WechatOfficalConfig.class.getName(), wechatOfficalConfig);
                }
            }
        }


    }


}
